package com.haiheng.module_main.mode

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.haiheng.library.log.LogUtils
import com.haiheng.library_base.model.BaseViewModel
import com.haiheng.library_base.db.BookInfoBean
import kotlinx.coroutines.launch

/**
 *
 * @Des:图书理架
 * @Author: hwz
 * @Date:2023/6/19 0019
 * @Version: 1.0
 */
class BookReShelvingViewModel(private val bookInfoViewModel: BookInfoViewModel,
                              private val bookRetirementViewModel: BookRetirementViewModel,
                              private val alarmedBooksViewModel: AlarmedBooksViewModel) : BaseViewModel() {
    var retirementNum = MutableLiveData<Int>().also { it.value = 0 }
    var alarmedNum = MutableLiveData<Int>().also { it.value = 0 }
    var onShelfNum = MutableLiveData<Int>().also { it.value = 0 }
    var wrongShelfNum = MutableLiveData<Int>().also { it.value = 0 }
    var notOnShelfNum = MutableLiveData<Int>().also { it.value = 0 }

    /**
     * 清除统计记录
     *
     */
    fun clearNum(){
        retirementNum.value = 0
        alarmedNum.value = 0
        onShelfNum.value = 0
        wrongShelfNum.value = 0
        notOnShelfNum.value = 0
    }

    fun updataRetirementAndAlarmedNum(){
        var retirment = 0
        var alarmed = 0
        bookInfoViewModel._bookListData.value?.forEach {
            val retirmentExists = bookRetirementViewModel._bookRetirementData.value!!.any {item ->
                item.objInfo.equals(it.barCode)
            }

            val alarmedExists = alarmedBooksViewModel._alarmedBooksData.value!!.any {item ->
                item.sinfo1.equals(it.barCode)
            }
            if (alarmedExists){
                it.isAlarmed = true
                alarmed++
            }
            if (retirmentExists){
                it.isRetirment = true
                retirment++
            }
        }
        alarmedNum.postValue(alarmed)
        retirementNum.postValue(retirment)
        if (bookInfoViewModel._bookListData.value != null){
            bookInfoViewModel._bookListData.postValue(bookInfoViewModel._bookListData.value)
        }
    }

    fun updataAlarmedNum(){
        var alarmed = 0
        bookInfoViewModel._bookListData.value?.forEach {
            val alarmedExists = alarmedBooksViewModel._alarmedBooksData.value!!.any {item ->
                item.sinfo1.equals(it.barCode)
            }
            if (alarmedExists){
                LogUtils.e("hwz 存在 报警书籍：${it.bookTitle}")
                it.isAlarmed = true
                alarmed++
            }

        }
        alarmedNum.postValue(alarmed)
        if (bookInfoViewModel._bookListData.value != null){
            bookInfoViewModel._bookListData.postValue(bookInfoViewModel._bookListData.value)
        }

    }

    fun updataNum(bookInto: BookInfoBean, curentLayerName:String){
        var isExists = false
        // 正确在架 1 错架 2  3不在架
        bookInfoViewModel._bookListData.value?.forEach {
            if (it.barCode.equals(bookInto.barCode)){
                isExists = true
                // 0 未识别过，正确在架 1 错架 2  不在架 3
                if (it.customReshelfStatus == 0){
//                    //未识别过标签，且存在列表中，则不在架次数减1
//                    notOnShelfNum.also { it.value = it.value?.plus(-1)}
                    if (curentLayerName.equals(it.fixedContainerSub?.name)){
                        it.customReshelfStatus = 1
                        onShelfNum.also { it.value = it.value?.plus(1)}
                    }else{
                        it.customReshelfStatus = 2
                        wrongShelfNum.also { it.value = it.value?.plus(1) }
                    }
                }else{
                    LogUtils.e("已经读到过的标签：${it.bookTitle}")
                    return
                }
            }
        }
        if (!isExists){//如果列表中没有查到，层架号不相同，则都表示错架，空标签也表示错架
            if (bookInto.fixedContainerSub != null
                && bookInto.fixedContainerSub?.name != null
                && curentLayerName.equals(bookInto.fixedContainerSub?.name)){
                bookInto.customReshelfStatus = 1
                onShelfNum.also { it.value = it.value?.plus(1)}
            }else{
                bookInto.customReshelfStatus = 2
                wrongShelfNum.also { it.value = it.value?.plus(1) }
            }
            viewModelScope.launch {
                bookInfoViewModel.addBook(bookInto)
//                calculateNotOnShelfNum()
            }
        }else{
//            calculateNotOnShelfNum()
        }

        LogUtils.e("在架：${onShelfNum.value}  错架：${wrongShelfNum.value}  未上架：${notOnShelfNum.value}")

        if (bookInfoViewModel._bookListData.value != null){
            bookInfoViewModel._bookListData.postValue(bookInfoViewModel._bookListData.value)
        }

    }

    /**
     * 计算不在架数量
     */
    fun calculateNotOnShelfNum() {
        notOnShelfNum.value = bookInfoViewModel._bookListData.value!!.size - onShelfNum.value!! - wrongShelfNum.value!!
    }
}